#include <unistd.h>
#include "db.h"
#include "common.h"


int main(int argc,char *argv[])
{
  int oc;
  item = NULL;
  label = NULL;

  if(access(CONFIG_FILE,F_OK) == -1)
  {
    fprintf(stderr,"Can't Open Configure File %s\n",CONFIG_FILE);
    exit(-1);
  }

  /* 检查参数个数 */
  if(argc < 2)
    use_age();

  /* 设定命令行参数接收参数 */
  while( (oc = getopt(argc,argv,"L:Ui:f:e:")) != -1 )
  {
    switch(oc)
    {
      /* L接收标签项，如db-1,db-2 */
      case 'L': 
        label = optarg;
        break;
      /* i接收item,即需要获取状态名，如qps,tps等等 */
      case 'i':
        item = optarg;
        break;
       /* e接收innodb_status里的参数，如buffer_pool_size等,必须和item一起出现*/
      case 'e':
        inno_status = optarg;
        break;
      default:
        use_age();
       /* 更新mysql的状态信息，将信息写入到指定文件中 */ 
      case 'U':
        update_mysql_status();
        innodb_status_value();
        exit(0);
    }

  }

  //printf("item = %s\n",item);

  /* 必须指定label和item */
  if((item == NULL) || (label == NULL))
    use_age();


  /* QPS*/
  if(strcmp(item,"qps") == 0)
  {
    float qps=get_mysql_qps();
    fprintf(stdout,"%0.2f\n",qps); 
  }

  /* Bytes_received  */
  if(strcmp(item,"net_rece") == 0)
  {
    char *bytes_rece = "Bytes_received";
    float net_rece=mysql_network_per_sec(bytes_rece);
    fprintf(stdout,"%0.2f\n",net_rece);
  }
  
  /* Bytes_sent */
  if(strcmp(item,"net_sent") == 0)
  {
    char *bytes_sent  = "Bytes_sent";
    float net_sent=mysql_network_per_sec(bytes_sent);
    fprintf(stdout,"%0.2f\n",net_sent);
  }
  
  /* The Time Slave Behind */
  if(strcmp(item,"delay_time") == 0)
  {
    int delay_time = get_slave_behind_master();
    fprintf(stdout,"%d\n",delay_time);
  }

  /* TPS */
  if(strcmp(item,"tps") == 0)
  {
    float tps=get_mysql_tps();
    fprintf(stdout,"%0.2f\n",tps);
  }

  /* Slow Query Log */
  if(strcmp(item,"slow_log") == 0)
  {
    float slowlog = get_mysql_slow_log_frequency();
    fprintf(stdout,"%0.2f\n",slowlog);
  }

  /* InnoDB Buffer read hit */
  if(strcmp(item,"inno_read_hit") == 0)
  {
    float inno_read_hit = get_inno_buffer_read_hit();
    fprintf(stdout,"%0.2f\n",inno_read_hit);
  }

  /* Threads_connected */
  if(strcmp(item,"threads_connected") == 0)
  {
    int threads_connected = get_threads_connected();
    fprintf(stdout,"%d\n",threads_connected);
  }

  /* Threads_running */
  if(strcmp(item,"threads_running") == 0)
  {
    int threads_running = get_threads_running();
    fprintf(stdout,"%d\n",threads_running);
  }

  /* Query Cache hit */
  if(strcmp(item,"qcache_hit") == 0)
  {
    float qcache_hit = get_query_cache_hit();
    fprintf(stdout,"%0.2f\n",qcache_hit);
  }

  /* Created_tmp_disk_tables */
  if(strcmp(item,"tmp_disk_tables") ==0)
  {
    float tmp_disk_tables_per_sec = get_disk_table_per_sec();
    fprintf(stdout,"%0.2f\n",tmp_disk_tables_per_sec);
  }

  /* get show engine InnoDB status */
  if(strcmp(item,"inno_stat") == 0 )
  {
    //innodb_status_value();
    float test = status_value(inno_status);
    printf("test = %0.2f\n",test);
  }

  return 0;
}
